package com.imay.live.fragment.discovery;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.activity.discovery.HotLabelActivity;
import com.imay.live.activity.discovery.HotLabelDetailActivity;
import com.imay.live.adapter.AssortmentLinearAdapter;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.helper.HotLabelCallBack;
import com.imay.live.helper.LabelViewMode;
import com.imay.live.helper.SuggestViewModel;
import com.imay.live.model.AssortmentModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/27.
 */

public class DisCoveryAssortFragment extends LoadFragment implements HotLabelCallBack {

    public final static int REQUE_REFRSH_TITLE = 1010;

    public static DisCoveryAssortFragment newInstance() {
        return new DisCoveryAssortFragment();
    }

    @Override
    public void setTabH(int tabH) {
        super.setTabH(tabH);
        addFooterView(tabH);
    }

    @Override
    public boolean getsetEnableRefresh() {
        if (mLinearRecyclerView != null) {
            RecyclerView.LayoutManager layoutManager = mLinearRecyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                int index = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
                return index == 0;
            }
        }
        return super.getsetEnableRefresh();
    }

    ArrayList<MultiItemEntity> toplist = new ArrayList<MultiItemEntity>();

    @Override
    public void onRequestSuccess(String command, String data) {
        completeLoad();
        try {
            Result<ArrayList<String>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<String>>>() {
            }.getType());
            if (result.result == ResultCode.CodeSuccess && result.data != null && result.data.size() > 0) {
//                        mdatas.clear();
                toplist.clear();
                AssortmentModel assortmentModel2 = new AssortmentModel();
                assortmentModel2.setItemType(AssortmentLinearAdapter.VIEWTYPE_LINEAR_AREA);
                toplist.add(0, assortmentModel2);
                SuggestViewModel suggestViewModel = new SuggestViewModel();
                suggestViewModel.setLabel(result.data);
                assortmentModel2.setLabels(result.data);
                toplist.add(1, suggestViewModel);
                getHotLabel();
            } else {
                loadError(getResources().getString(R.string.str_loaderror));
            }

        } catch (Exception e) {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        loadError(getResources().getString(R.string.str_loaderror));
    }

//    @Override
//    public void onRefresh() {
//        super.onRefresh();
//        ProtoRequestHelper.task(ProtoHttpFactory.getSuggestLabel()).setTag(this.hashCode()).request(new ProtoCallBack() {
//            @Override
//            public void onRequestSuccess(String command, String data) {
//                DisCoveryAssortFragment.this.onRequestSuccess(command, data);
//                ((MainActivity) getActivity()).saveDiscoveryData(1, data);
//
//            }
//
//            @Override
//            public void onRequestError(String command, int code, String message) {
//                DisCoveryAssortFragment.this.onRequestError(command, code, message);
//            }
//        });
//
//    }

    private void getHotLabel() {
        ProtoRequestHelper.task(ProtoHttpFactory.getHotLabel()).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<String>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<String>>>() {
                    }.getType());
                    if (result.result == ResultCode.CodeSuccess && result.data != null) {
                        mdatas.clear();
                        mdatas.addAll(toplist);
                        AssortmentModel assortmentModel0 = new AssortmentModel();
                        assortmentModel0.setItemType(AssortmentLinearAdapter.VIEWTYPE_LINEAR_HOT);
                        mdatas.add(assortmentModel0);
                        int i = 0;
                        for (String label : result.data) {
                            if (TextUtils.isEmpty(label)) {
                                continue;
                            }
                            LabelViewMode labelViewMode = new LabelViewMode();
                            if (i == 0) {
                                labelViewMode.setStatus("HOT");
                            }
                            i++;
                            labelViewMode.setLabel(label);
                            mdatas.add(labelViewMode);
                        }
                        if (mdatas.size() == 0) {
                            loadError(getResources().getString(R.string.str_loaderror));
                        } else {
                            if (mdatas.size() > 5) {
                                addFooterView(tabH);
                            }
                        }

                        mAdapter.notifyDataSetChanged();
                    } else {
                        loadError(getResources().getString(R.string.str_loaderror));
                    }

                } catch (Exception e) {
                    loadError(getResources().getString(R.string.str_loaderror));
                }


            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadError(getResources().getString(R.string.str_loaderror));
            }
        });
    }

    @Override
    public void onLoadMore() {
        mRefreshLayout.finishLoadmore();
    }

    @Override
    void initAdapte() {
        mAdapter = new AssortmentLinearAdapter(mdatas, this);
        mLinearRecyclerView.setAdapter(mAdapter);
        mLinearRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .size(0)
                .build());
    }


    @Override
    void oncreate() {
        super.oncreate();
        addFooterView(tabH);
        MobclickAgent.onEvent(getActivity(), "find_classify");
    }

    @Override
    public void onClickTitle(String title) {
        HotLabelDetailActivity.start(getActivity(), title);
    }

    @Override
    public void onClickSuggest(ArrayList<String> labels) {
        HotLabelActivity.start(getActivity(), labels);
    }
}
